package com.apartment.control;

import com.apartment.data.entity.Apartment;
import com.apartment.data.entity.Indent;
import com.apartment.data.entity.User;
import com.apartment.data.view.Indentview;
import com.apartment.sevice.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Author : xuzhiyong
 * Date : 2022/5/27 17:50
 */
@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private ApartmentService apartmentService;

    @Autowired
    private IndentviewService indentviewService;


    @RequestMapping("/toAdd")
    public String toAdd() {
        return "order/orderAdd";
    }

    @RequestMapping("/addIndent")
    public String add(Indent indent){
        orderService.addIndent(indent);
        return "redirect:/order/orderList";
    }

    @RequestMapping("/toAddOrderForUser")
    public String toAddOrderForUser(Model model,Integer uid,Integer aid){
        User user = userService.findUserById(uid);
        model.addAttribute("user",user);
        Apartment apartment = apartmentService.findApartmentById(aid);
        model.addAttribute("apartment",apartment);
        return "order/orderAddForUser";
    }

    @RequestMapping("/addOrderForUser")
    public String addOrderForUser(Model model,Integer id,Indent indent){
        orderService.addIndent(indent);
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        List<Indent> indents = orderService.findIndentsByUserId(id);
        model.addAttribute("indents",indents);
        return "order/orderListForUserId";
    }

    //查看历史订单
    @RequestMapping("/historyIndents")
    public String hirstory(Model model,Integer id){
        User user = userService.findUserById(id);
        model.addAttribute("user",user);
        List<Indent> indents = orderService.findIndentByUserId(id);
        model.addAttribute("indents",indents);
        return "order/orderForHistory";
    }

    //查看订单详情
    @RequestMapping("/orderDetail")
    public String addIndentForUser(Model model,Integer id){
        Indentview indentview = indentviewService.findIndentviewById(id);
        model.addAttribute("indentview",indentview);
        return "order/orderDetail";
    }

    @RequestMapping("/indent")
    @ResponseBody
    public List<Indent> indents(@RequestParam("id") Integer id){
        return orderService.findIndentByUserId(id);
    }

    @RequestMapping("/orderList")
    public String orderList(Model model){
        System.out.println("");
        List<Indent> indents = orderService.findAllIndent();
        model.addAttribute("indents",indents);
        return "order/orderList";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        orderService.deleteIndentById(id);
        return "redirect:/order/orderList";
    }

}
